package com.lzh.chatnote.common.mqConsumer;


import com.lzh.chatnote.chat.dao.ContactDao;
import com.lzh.chatnote.chat.dao.MessageDao;
import com.lzh.chatnote.chat.dao.RoomDao;
import com.lzh.chatnote.chat.dao.RoomFriendDao;
import com.lzh.chatnote.chat.domain.entity.*;
import com.lzh.chatnote.chat.domain.vo.response.message.MessageResp;
import com.lzh.chatnote.chat.service.MessageService;
import com.lzh.chatnote.chat.service.cache.*;
import com.lzh.chatnote.chat.service.impl.PushService;
import com.lzh.chatnote.common.constant.MQConstant;
import com.lzh.chatnote.webSocket.service.adapter.WSAdapter;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 用户发送消息mq消费
 */
@RocketMQMessageListener(consumerGroup = MQConstant.SEND_MSG_GROUP,
        topic = MQConstant.SEND_MSG_TOPIC, messageModel = MessageModel.BROADCASTING)
@Component
public class MsgSendConsumer implements RocketMQListener<Long> {

    @Resource
    private MessageDao messageDao;
    @Resource
    private RoomCache roomCache;
    @Resource
    private RoomDao roomDao;
    @Resource
    private MessageService messageService;
    @Resource
    private HotRoomCache hotRoomCache;
    @Resource
    private PushService pushService;
    @Resource
    private RoomGroupCache roomGroupCache;
    @Resource
    private GroupMemberCache groupMemberCache;
    @Resource
    private RoomFriendCache roomFriendCache;
    @Resource
    private ContactDao contactDao;

    @Override
    public void onMessage(Long msgId) {
        //发送消息
        Message message = messageDao.getById(msgId);
        Room room = roomCache.getOne(message.getRoomId());
        //刷新房间最后一消息id和最后一条时间
        roomDao.reflushLastMsgIdAndTime(room.getId(), message.getId(), message.getCreateTime());
        //room缓存实效
        roomCache.delOne(message.getRoomId());
        //根据message获取封装返回的vo
        MessageResp messageResp = messageService.getMsgResp(Collections.singletonList(message),null);
        if(room.isHotRoom()){
            //热聊房间
            //刷新热点房间，最新消息的时间，通过消息id作为刷新（消息表是单调递增的消息）
            hotRoomCache.refreshHotRoomNewMsg(room.getId(), message.getId());
            //推送给mq，mq推送给所有用户
            pushService.pushMsgAllUser(WSAdapter.buildUserSendMsgResp(messageResp));
        }else{
            List<Long> allUserId = Collections.emptyList();
            //非热点房间
            if(room.isSingleRoom()){
                //1.单聊房间
                RoomFriend roomFriend = roomFriendCache.getOne(room.getId());
                allUserId = Arrays.asList(roomFriend.getUid1(), roomFriend.getUid2());

            }else if (room.isGroupRoom()){
                //2.群聊房间
                RoomGroup roomGroup = roomGroupCache.getOne(room.getId());
                List<GroupMember> groupMembers = groupMemberCache.getOne(roomGroup.getId());
                allUserId = groupMembers.stream().map(GroupMember::getUid).collect(Collectors.toList());
            }

            //刷新会话表时间，实现刷新用户列表会话排序
            contactDao.refreshLastMsgIdOrSave(room.getId(),message.getId(),allUserId);

            //推送消息给mq，mq推送给群里在线的用户
            pushService.pushMsgRoomUser(WSAdapter.buildUserSendMsgResp(messageResp),allUserId);
        }
    }
}
